package lunchy.data.classes;

import java.util.Date;

import org.hibernate.Session;

import lunchy.data.interfaces.ILunchyMenuGroup;
import lunchy.data.interfaces.ILunchyOrder;
import lunchy.data.interfaces.ILunchyOrderDAO;
import lunchy.data.interfaces.ILunchyOrderItem;
import lunchy.util.CLunchyHibernateUtil;

public class CLunchyDBOrderDAO implements ILunchyOrderDAO
{
	private Session session = null;
	
	public CLunchyDBOrderDAO()
	{
		session = CLunchyHibernateUtil.getSessionFactory().openSession();
	}
	
	public void addOrder(ILunchyOrder orderToSave)
	{
		long orderID = System.currentTimeMillis()/1000;
		
		orderToSave.setOrderID(orderID);
		
		try
		{
			session.beginTransaction();
			
			session.save(orderToSave);

			for (ILunchyOrderItem orderItem : orderToSave.getOrderList())
			{
				orderItem.setOrderID(orderID);
				
				session.save(orderItem);
			}

			session.getTransaction().commit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
